/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package doan;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;


/**
 *
 * @author Windows 7
 */
public class Form1 extends javax.swing.JFrame {

    /**
     * Creates new form Form1
     */
    public Form1() throws Exception {
        initComponents();
        LoadDB();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pn1 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        bt1_dangnhap = new javax.swing.JButton();
        tf1_username = new javax.swing.JTextField();
        tf1_pass = new javax.swing.JPasswordField();
        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setBounds(new java.awt.Rectangle(0, 0, 800, 600));
        setMaximizedBounds(new java.awt.Rectangle(0, 0, 800, 600));
        setMinimumSize(new java.awt.Dimension(800, 620));
        getContentPane().setLayout(null);

        pn1.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel2.setFont(new java.awt.Font("Tahoma", 2, 12)); // NOI18N
        jLabel2.setForeground(new java.awt.Color(0, 0, 255));
        jLabel2.setText("Bạn đang cần gì?");
        pn1.add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(490, 540, 100, -1));

        bt1_dangnhap.setBackground(new java.awt.Color(204, 204, 204));
        bt1_dangnhap.setFont(new java.awt.Font("Times New Roman", 2, 18)); // NOI18N
        bt1_dangnhap.setForeground(new java.awt.Color(51, 102, 255));
        bt1_dangnhap.setText("Đăng Nhập");
        bt1_dangnhap.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt1_dangnhapActionPerformed(evt);
            }
        });
        pn1.add(bt1_dangnhap, new org.netbeans.lib.awtextra.AbsoluteConstraints(320, 470, 139, 50));

        tf1_username.setFont(new java.awt.Font("Tahoma", 2, 18)); // NOI18N
        tf1_username.setText("Tên Đăng Nhập");
        tf1_username.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tf1_usernameActionPerformed(evt);
            }
        });
        pn1.add(tf1_username, new org.netbeans.lib.awtextra.AbsoluteConstraints(260, 340, 260, 60));

        tf1_pass.setText("jPasswordField1");
        pn1.add(tf1_pass, new org.netbeans.lib.awtextra.AbsoluteConstraints(260, 400, 260, 60));

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/doan/silde1.jpg"))); // NOI18N
        jLabel1.setAutoscrolls(true);
        pn1.add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(-10, 0, 800, -1));

        getContentPane().add(pn1);
        pn1.setBounds(0, 0, 800, 600);

        pack();
    }// </editor-fold>//GEN-END:initComponents
public boolean LoadDB() throws Exception{
		try{
		Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                String url ="Jdbc:odbc:doan";
		Connection conn = DriverManager.getConnection(url);
		Statement stt=conn.createStatement();
                String sql="Select * from User";
		ResultSet rs = stt.executeQuery(sql);
		while (rs.next()){
                    String n=rs.getString("name");
                    String p=rs.getString("password");
                    //String tringone=tf1_username.getText();                   
                    int a = tf1_username.getText().compareTo(n);
                    int b = tf1_pass.getText().compareTo(p);
                    //System.out.println(b);
                    if((a==0) &&(b==0))
    
                        return true;
                }
		}catch (SQLException e) {
			System.out.println("Loi thao tac CSDL");
		}
		return false;
	}
    private void bt1_dangnhapActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt1_dangnhapActionPerformed
        try {      
            //  if ()
            //  if ((tf1_username.getText()=="admin")&&(tf1_pass.getText()=="123456"))           
            if(LoadDB()){
                this.setVisible(false) ;
                Form2 t= new Form2();
                t.setVisible(true);           
            } 
            else
                JOptionPane.showMessageDialog(null, "Error!!");
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null, "Loi Roi");
        }
       
    }//GEN-LAST:event_bt1_dangnhapActionPerformed

    private void tf1_usernameActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tf1_usernameActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_tf1_usernameActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Form1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Form1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Form1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Form1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    new Form1().setVisible(true);
                } catch (Exception ex) {
                    Logger.getLogger(Form1.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bt1_dangnhap;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel pn1;
    private javax.swing.JPasswordField tf1_pass;
    private javax.swing.JTextField tf1_username;
    // End of variables declaration//GEN-END:variables
}
